package lcr;

import java.util.Comparator;
import java.util.TreeSet;

public class Test0058 {
}

class MyCalendar {

    TreeSet<int[]> booked;

    public MyCalendar() {
        booked= new TreeSet<>(Comparator.comparingInt(a -> a[0]));
    }

    public boolean book(int start, int end) {
        int[] a = {start, end};
        if (booked.isEmpty()){
            booked.add(a);
            return true;
        }

        int[] array1 = booked.ceiling(new int[]{end});
        int[] array2 = array1 == null ? booked.last() : booked.lower(array1);

        if ((array1 == null || array1[0] >= end) &&
                (array2 == null || array2[1] <= start)){
            booked.add(a);
            return true;
        }
        return false;
    }
}